"""API路由统一入口"""

from fastapi import FastAPI, WebSocket
from typing import List, Any
from backend.common.config import config_manager

# 导入路由模块
def get_routers() -> List[Any]:
    """获取所有API路由器"""
    from backend.api.routers.chat import router as chat_router
    from backend.api.routers.rag import router as rag_router
    from backend.api.routers.tools import router as tools_router
    from backend.api.routers.admin import router as admin_router
    from backend.api.routers.session import router as session_router
    from backend.api.routers.settings import router as settings_router
    from backend.api.routers.excel import router as excel_router
    from backend.api.routers.datasets import router as datasets_router
    from backend.api.routers.ollama import router as ollama_router
    from backend.api.routers.data_tools import router as data_tools_router
    from backend.api.routers.auth import router as auth_router
    
    return [
        chat_router,
        rag_router,
        tools_router,
        admin_router,
        session_router,
        settings_router,
        excel_router,
        datasets_router,
        ollama_router,
        data_tools_router,
        auth_router,
    ]

def register_routers(app: FastAPI) -> None:
    """注册所有API路由器到FastAPI应用"""
    # 获取所有路由器
    routers = get_routers()
    
    # 注册每个路由器，使用路由器自己的前缀（已经包含了/v1/）
    for router in routers:
        app.include_router(router)
    
    # 导入WebSocket处理函数
    from backend.api.websocket_chat import handle_websocket_chat
    
    # 注册WebSocket端点
    @app.websocket("/ws/chat")
    async def websocket_endpoint(websocket: WebSocket):
        await handle_websocket_chat(websocket, config_manager)

__all__ = [
    'get_routers',
    'register_routers'
]
